home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / AOCE / Development Tools / Sample Code / Messaging Service Access Module / Internet PMSAM / Internet PMSAM source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  4.8 KB  |  238 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------
  2.  
  3. AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
  4. Mail Service Access Module
  5.  
  6. written by Steve Falkenburg-- MacDTS
  7. ©1991-1993 Apple Computer, Inc.
  8.  
  9. --------------
  10. change history
  11. --------------
  12.  
  13. SJF        02/19/93    update for beta build    b1
  14. SJF        10/29/92    update to a11            a11
  15. SJF        06/08/92    update to a8            a8
  16. SJF        02/15/92    first working version    a4.5
  17. SJF        10/16/91    initial coding            a3
  18.  
  19. ---------------------------------------------------------------------*/
  20.  
  21. #ifndef __TYPES__
  22. #include <Types.h>
  23. #endif
  24.  
  25. #ifndef __MEMORY__
  26. #include <Memory.h>
  27. #endif
  28.  
  29. #ifndef __SEGLOAD__
  30. #include <SegLoad.h>
  31. #endif
  32.  
  33. #ifndef __TRAPS__
  34. #include <Traps.h>
  35. #endif
  36.  
  37. #ifndef __EVENTS__
  38. #include <Events.h>
  39. #endif
  40.  
  41. #ifndef __EPPC_
  42. #include <EPPC.h>
  43. #endif
  44.  
  45. #ifdef applec
  46. #include <SysEqu.h>
  47. #endif
  48.  
  49. #include "const.h"
  50. #include "gwerrors.h"
  51. #include "globals.h"
  52. #include "network.h"
  53. #include "gatewaystuff.h"
  54. #include "trapavailable.h"
  55. #include "utils.h"
  56. #include "myevents.h"
  57. #include "spoolsystem.h"
  58. #include "authstuff.h"
  59. #include "gatewayevents.h"
  60. #include "main.h"
  61.  
  62. #ifdef __SYSEQU__
  63. #define DefaultStackSize    *((long *)DefltStack)
  64. #define StackBase            *((Ptr *)CurStackBase)
  65. #else
  66. #define DefaultStackSize    DefltStack
  67. #define StackBase            CurStackBase
  68. #endif
  69.  
  70. // main
  71. //
  72. // main entry point- initializes program components and
  73. // calls main event loop.  exits via ExitToShell if gateway can't initialize
  74. //
  75. void main(void)
  76. {
  77.     OSErr err;
  78.     
  79.     TraceExecution("\pGATEWAY LAUNCH");
  80.     
  81.     if (!HasAOCE()) 
  82.         ExitToShell();
  83.         
  84.     err = InitMacStuff();
  85.     if (err!=noErr) {
  86.         DoError(err);
  87.         ExitToShell();
  88.     }
  89.         
  90.     err = InitSpoolSubsystem();
  91.     if (err!=noErr) {
  92.         DoError(err);
  93.         ExitToShell();
  94.     }
  95.  
  96.     gNetworkInited = false;
  97.     InitRemoteNetStuff();
  98.     // don't exit if we get an error, we'll run anyway, but won't route mail
  99.         
  100.     err = InitAOCEStuff();
  101.     if (err!=noErr) {
  102.         DoError(err);
  103.         ExitToShell();
  104.     }
  105.         
  106.     err = InitGatewayStuff();
  107.     if (err!=noErr) {
  108.         DoError(err);
  109.         ExitToShell();
  110.     }
  111.         
  112.     MainLoop();
  113.     ExitProc();
  114. }
  115.  
  116.  
  117. // ExitProc
  118. //
  119. // called before the gateway terminates.  this call insures that everything associated
  120. // with AOCE and external networking stuff is closed and released.
  121. //
  122. void ExitProc(void)
  123. {
  124.     TraceExecution("\pGATEWAY QUIT");
  125.     
  126.     CloseGatewayStuff();
  127.     CloseRemoteNetStuff();
  128.     CloseAOCEStuff();
  129.     
  130.     ExitToShell();
  131. }    
  132.  
  133.  
  134. // InitMacStuff
  135. //
  136. // called to initialize the Mac toolbox stuff required for the gateway, which is a background
  137. // only application.
  138. //
  139. OSErr InitMacStuff(void)
  140. {
  141.     SetApplLimit((Ptr) ((long) StackBase - (long) 0x6000));    // workaround for DefltStackSize lomem being munged
  142.     MaxApplZone();
  143.     MoreMasters();
  144.     MoreMasters();
  145.     InitGraf(&qd.thePort);        // need this to generate random numbers
  146.     
  147.     gAOCEInited = false;
  148.     gDone = false;
  149.     gWakeUp = gWakeUpSecondary = false;
  150.     
  151.     if (!TrapAvailable(_WaitNextEvent))
  152.         return kCantRun;
  153.     
  154.     if (!SupportsAEVT())
  155.         return kCantRun;
  156.     
  157.     GetCurrentProcess(&gOurPSN);
  158.     
  159.     return noErr;
  160. }
  161.  
  162.  
  163. // MainLoop
  164. //
  165. // this is the main event loop for the gateway application.  the sleep time will be set to the
  166. // maximum value (-1L) so that we never take any idle time and only wake up when we're sent
  167. // an event.
  168. //
  169. // *** SPECIAL NOTE:
  170. //
  171. // it's not strictly necessary to have this event loop at all.  the toolbox will re-launch the
  172. // gateway whenenver it actually needs to have the gateway to something.  i've chosen to keep the
  173. // gateway running at all times, since i read in the slot information into a global area and also
  174. // to insure that i will always have enough memory to activate.  this is probably not the optimal
  175. // strategy, but it is only sample code...
  176. // 
  177. void MainLoop(void)
  178. {
  179.     EventRecord ev;
  180.     Boolean gotEvt;
  181.     OSErr err;
  182.     
  183.     while (gDone==false) {
  184.         
  185.         // see if we need to refresh slot info
  186.         err = CheckSlotRefresh();
  187.         if (err!=noErr)
  188.             DoError(err);
  189.         
  190.         // see if we need to refresh identity stuff (in response to auth q notification)    
  191.         err = CheckAuthRefresh();    
  192.         if (err!=noErr)
  193.             DoError(err);
  194.  
  195.         err = ProcessQueuedEvents();
  196.         if (err!=noErr)
  197.             DoError(err);
  198.         
  199.         gotEvt = false;
  200.         if (gWakeUp==false)
  201.             gotEvt = WaitNextEvent(everyEvent,&ev,kSleepTime,nil);
  202.         gWakeUp = false;
  203.         if (gotEvt) {
  204.             err = HandleEvent(&ev,true);
  205.             if (err!=noErr)
  206.                 DoError(err);
  207.         }
  208.     }
  209. }
  210.  
  211.  
  212.  
  213. // SecondaryEventHandler
  214. //
  215. // this event handler is what we spin on when waiting for an async call to complete
  216. // we will process a subset of the EPPCs from here, namely: CreateSlot, ModifySlot, DeleteSlot
  217. // all other EPPCs are queued onto a waiting to be processed event queue
  218. // (not re-entrant)
  219. //
  220. void SecondaryEventLoop(void)
  221. {
  222.     EventRecord ev;
  223.     Boolean gotEvt;
  224.     OSErr err;
  225.  
  226.     gotEvt = false;
  227.     
  228.     if (gWakeUpSecondary==false) {
  229.         gotEvt = WaitNextEvent(everyEvent,&ev,kSecondarySleepTime,nil);
  230.     }
  231.     
  232.     if (gotEvt) {
  233.         err = HandleEvent(&ev,false);
  234.         if (err!=noErr)
  235.             DoError(err);
  236.     }
  237. }
  238.